package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCardMapper;
import com.sky.service.ShoppingCardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCardServiceImpl implements ShoppingCardService {

    private final ShoppingCardMapper shoppingCartMapper;

    private final DishMapper dishMapper;
    private final SetmealMapper setmealMapper;

    public ShoppingCardServiceImpl(ShoppingCardMapper shoppingCartMapper, DishMapper dishMapper, SetmealMapper setmealMapper) {
        this.shoppingCartMapper = shoppingCartMapper;
        this.dishMapper = dishMapper;
        this.setmealMapper = setmealMapper;
    }

    @Override
    public void add(ShoppingCartDTO shoppingCardDTO) {
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCardDTO, cart);
        cart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(cart);

        if (list != null && !list.isEmpty()) {
            ShoppingCart cartInDB = list.get(0);
            cartInDB.setNumber(cartInDB.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cartInDB);
        }else {
            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
            if (shoppingCardDTO.getDishId() != null){
                Dish byId = dishMapper.getById(shoppingCardDTO.getDishId());
                cart.setName(byId.getName());
                cart.setImage(byId.getImage());
                cart.setAmount(byId.getPrice());
            }else {
                Setmeal byId = setmealMapper.getById(shoppingCardDTO.getSetmealId());
                cart.setName(byId.getName());
                cart.setImage(byId.getImage());
                cart.setAmount(byId.getPrice());
                cart.setDishFlavor(shoppingCardDTO.getDishFlavor());
            }

            shoppingCartMapper.insert(cart);
        }
    }

    @Override
    public List<ShoppingCart> list() {
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(currentId).build();
        return shoppingCartMapper.list(shoppingCart);
    }

    @Override
    public void clear() {
        log.info("清空购物车");
        shoppingCartMapper.cleanByUserId(BaseContext.getCurrentId());
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, cart);
        cart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(cart);
        if (list != null && !list.isEmpty()) {
            ShoppingCart cartInDB = list.get(0);
            cartInDB.setNumber(cartInDB.getNumber() - 1);

            shoppingCartMapper.updateNumberById(cartInDB);
            int collect = list.stream().mapToInt(ShoppingCart::getNumber).sum();
            if ( collect == 0)clear();
        }

    }
}
